export function timeFix() {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome() {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent() {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader(callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () { }
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE() {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate(id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}
export function scorePassword(pass) {
  let score = 0
  if (!pass) {
    return score
  }
  // award every unique letter until 5 repetitions
  const letters = {}
  for (let i = 0; i < pass.length; i++) {
    letters[pass[i]] = (letters[pass[i]] || 0) + 1
    score += 5.0 / letters[pass[i]]
  }

  // bonus points for mixing it up
  const variations = {
    digits: /\d/.test(pass),
    lower: /[a-z]/.test(pass),
    upper: /[A-Z]/.test(pass),
    nonWords: /\W/.test(pass)
  }

  let variationCount = 0
  for (var check in variations) {
    variationCount += (variations[check] === true) ? 1 : 0
  }
  score += (variationCount - 1) * 10

  return parseInt(score)
}


export let urls = 'https://file.sytcul.cn/'
export let urlspath = 'https://s7dlc6q8.xiaomy.net/eroper/'

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
//递归树
export function getTreeData(arr, data, city = []) {
  // console.log(arr,data);
  if (typeof data === "object") {
    for (let i = 0; arr[i] !== undefined; i++) {
      for (let j = 0; data[j] !== undefined; j++) {
        if (arr[i] === data[j].id) {
          city.push(data[j]);
        }
      }
    };
    for (let i = 0; data[i] !== undefined; i++) {
      getTreeData(arr, data[i].childList, city);
    };
  }
  return city;
}
//递归key树
export function getTreeDatakey(arr, data, city = []) {
  if (typeof data === "object") {
    for (let i = 0; arr[i] !== undefined; i++) {
      for (let j = 0; data[j] !== undefined; j++) {
        if (arr[i] === data[j].key) {
          console.log(data[j]);
          city.push(data[j]);
        }
      }
    };
    for (let i = 0; data[i] !== undefined; i++) {
      getTreeDatakey(arr, data[i].childList, city);
    };
  }
  return city;
}
//递归树里的所有子级
export function getchild(data, city = []) {
  for (var i = 0; i < data.length; i++) {
    if (data[i].childList.length < 1) {
      city.push(data[i]);
    } else {
      city.push(data[i]);
      getchild(data[i].childList, city);
    }
  }
  return city;
}
//递归树里的所有子级
export function getdisabled(data) {
  console.log(data);
  for (var i = 0; i < data.length; i++) {
    if (data[i].type == 2) {
      data[i].disabled = false
    } else {
      data[i].disabled = true
    }
    if (data[i].childList.length != 0) {
      getdisabled(data[i].childList);
    }
  }
  return data;
} 

export function decimals(num, n) {
  n = n ? parseInt(n) : 0
  if (n <= 0) {
    return Math.round(num)
  }
  num = Math.round(num * 10 ** n) / 10 ** n // 四舍五入
  num = Number(num).toFixed(n) // 补足位数
  return num
}
